import 'package:flutter/material.dart';
import 'package:mybase/base/animate_do/types/animate_do_mixins.dart';
import 'package:mybase/base/animate_do/types/animate_do_types.dart';

//注意 key值一定要赋
@deprecated
class FFAnimate extends StatefulWidget {
  // 时长
  Duration duration;

  // 延迟
  Duration delay;

  // 是否无限循环
  bool infinite;

  // 是否手动触发
  bool manualTrigger;

  // 自动开始
  bool autoStart;

  // 控制器
  Function(AnimationController controller)? controller;

  // true:正向执行一次 false:反向执行一次
  bool animate;

  // 动画结束回调
  Function(AnimateDoDirection direction)? onFinish;

  // 动画到达结束回调
  Function(AnimationController controller)? onToEnd;

  // 动画回到起点回调
  Function(AnimationController controller)? onToBegin;

  // 曲线
  // Curves.easeIn: 逐渐加速
  // Curves.easeOut: 逐渐减速
  final Curve curve;

  // 开始值
  double begin;

  // 结束值
  double end;

  Function(
    BuildContext context,
    AnimationController controller,
    Widget? child,
    double progress,
  ) builder;

  FFAnimate(
      {required this.builder,
      Key? key,
      this.duration = const Duration(milliseconds: 1000),
      this.delay = const Duration(milliseconds: 0),
      this.autoStart = true,
      this.infinite = false,
      this.controller,
      this.manualTrigger = false,
      this.animate = true,
      this.begin = 0,
      this.end = 1,
      this.onFinish,
      this.onToEnd,
      this.onToBegin,
      this.curve = Curves.linear})
      : super(key: key) {
    if (manualTrigger == true && controller == null) {
      throw FlutterError('如果你想使用 manualTrigger:true， \n\n'
          '你必须实现回调 Function(AnimationController controller)? controller. \n\n');
    }
  }

  @override
  State<FFAnimate> createState() => _FFAnimateState();
}

class _FFAnimateState extends State<FFAnimate>
    with SingleTickerProviderStateMixin, AnimateDoState {
  late Animation<double> animation;

  @override
  void dispose() {
    disposed = true;
    controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    controller = AnimationController(duration: widget.duration, vsync: this);
    animation = Tween<double>(begin: widget.begin, end: widget.end)
        .animate(CurvedAnimation(parent: controller, curve: widget.curve));

    configAnimation(
      autoStart: widget.autoStart,
      delay: widget.delay,
      animate: widget.animate,
      manualTrigger: widget.manualTrigger,
      infinite: widget.infinite,
      onFinish: widget.onFinish,
      controllerCallback: widget.controller,
      onToEnd: widget.onToEnd,
      onToBegin: widget.onToBegin,
    );
  }

  @override
  Widget build(BuildContext context) {
    buildAnimation(
      autoStart: widget.autoStart,
      delay: widget.delay,
      animate: widget.animate,
      manualTrigger: widget.manualTrigger,
      infinite: widget.infinite,
      onFinish: widget.onFinish,
      controllerCallback: widget.controller,
    );

    return AnimatedBuilder(
      animation: controller,
      builder: (BuildContext context, Widget? child) {
        return widget.builder(context, controller, child, animation.value);
      },
    );
  }
}
